package com.data.permission.rule.process;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.data.permission.constrant.EnabledEnum;
import com.data.permission.rule.dto.ConditionDetailDTO;
import com.data.permission.rule.dto.RuleConditionDTO;
import com.data.permission.rule.dto.RuleInfoDTO;
import com.data.permission.rule.entity.RuleConditionDetail;
import com.data.permission.rule.entity.RuleConditionInfo;
import com.data.permission.rule.entity.RuleInfo;
import com.data.permission.rule.service.IRuleConditionDetailService;
import com.data.permission.rule.service.IRuleConditionInfoService;
import com.data.permission.rule.service.IRuleInfoService;
import com.data.permission.transf.DataPermissionsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.TException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by 张波 on 2019/11/3.
 */
@Service
@Slf4j
public class PermissionProcess implements DataPermissionsService.Iface {

    @Autowired
    private IRuleInfoService ruleInfoService;

    @Autowired
    private IRuleConditionInfoService ruleConditionInfoService;

    @Autowired
    private IRuleConditionDetailService ruleConditionDetailService;

    @Override
    public void ping() throws TException {
        log.debug("服务检测");
    }

    /**
     * 规则刷新
     *
     * @return
     */
    @Override
    public Map<String, RuleInfoDTO> flushRuleInfo() {
        /*1、加载所有的数据规则*/
        List<RuleInfo> ruleInfoList = ruleInfoService.list(new QueryWrapper<>(new RuleInfo().setIsEnable(EnabledEnum.ENABLED.getCode())));
        Map<String, RuleInfoDTO> ruleInfoMap = new HashMap<>(ruleInfoList.size());
        if (ruleInfoList.isEmpty()) {
            return Collections.unmodifiableMap(ruleInfoMap);
        }
        RuleInfoDTO ruleInfo = null;
        RuleConditionDTO ruleConditionDTO = null;
        ConditionDetailDTO conditionDetailDTO = null;
        for (RuleInfo rule : ruleInfoList) {
            ruleInfo = new RuleInfoDTO();
            ruleInfo.setRuleCode(rule.getRuleCode());
            ruleInfo.setRuleName(rule.getRuleName());
            ruleInfo.setConditionList(new ArrayList<>());
            ruleInfoMap.put(rule.getRuleCode(), ruleInfo);
            /*2、根据规则查找规则所对应的条件列表*/
            List<RuleConditionInfo> ruleConditionInfoList = ruleConditionInfoService.list(new QueryWrapper<>(new RuleConditionInfo().setRuleCode(ruleInfo.getRuleCode()).setIsEnable(EnabledEnum.ENABLED.getCode())));
            if (!ruleConditionInfoList.isEmpty()) {
                for (RuleConditionInfo conditionInfo : ruleConditionInfoList) {
                    ruleConditionDTO = new RuleConditionDTO();
                    ruleConditionDTO.setConditionName(conditionInfo.getConditionName());
                    ruleConditionDTO.setConditionCode(conditionInfo.getConditionCode());
                    ruleConditionDTO.setConditionDetailList(new ArrayList<>());
                    ruleInfo.getConditionList().add(ruleConditionDTO);
                    /*3、根据规则查找规则对应的明细*/
                    List<RuleConditionDetail> conditionDetailList = ruleConditionDetailService.list(new QueryWrapper<>(new RuleConditionDetail().setConditionCode(ruleConditionDTO.getConditionCode())).orderByAsc("CONDITION_ORDER"));
                    if (!conditionDetailList.isEmpty()) {
                        for (RuleConditionDetail detail : conditionDetailList) {
                            conditionDetailDTO = new ConditionDetailDTO();
                            conditionDetailDTO.setConditionDetailCode(detail.getConditionCode());
                            conditionDetailDTO.setConditionField(detail.getConditionField());
                            conditionDetailDTO.setConditionFlag(detail.getConditionFlag());
                            conditionDetailDTO.setCheckType(detail.getCheckType());
                            conditionDetailDTO.setSourceType(detail.getSourceType());
                            conditionDetailDTO.setDataSource(detail.getDataSource());
                            conditionDetailDTO.setConditionOrder(detail.getConditionOrder());
                            ruleConditionDTO.getConditionDetailList().add(conditionDetailDTO);
                        }
                    }
                }
            }
        }
        /*将期修饰为不可变的List，提供只读*/
        return Collections.unmodifiableMap(ruleInfoMap);
    }

    /**
     * 加载所有的条件对象
     *
     * @return
     */
    @Override
    public Map<String, RuleConditionDTO> flushConditionMap() {
        Map<String, RuleConditionDTO> ruleConditionInfoMap = new HashMap<>();
        /*1、加载所有的条件*/
        RuleConditionDTO ruleConditionDTO = null;
        ConditionDetailDTO conditionDetailDTO = null;
        List<RuleConditionInfo> ruleConditionInfoList = ruleConditionInfoService.list(new QueryWrapper<>(new RuleConditionInfo().setIsEnable(EnabledEnum.ENABLED.getCode())));
        if (!ruleConditionInfoList.isEmpty()) {
            for (RuleConditionInfo conditionInfo : ruleConditionInfoList) {
                ruleConditionDTO = new RuleConditionDTO();
                ruleConditionDTO.setConditionName(conditionInfo.getConditionName());
                ruleConditionDTO.setConditionCode(conditionInfo.getConditionCode());
                ruleConditionDTO.setConditionDetailList(new ArrayList<>());
                ruleConditionInfoMap.put(conditionInfo.getConditionCode(), ruleConditionDTO);
                    /*3、根据规则查找规则对应的明细*/
                List<RuleConditionDetail> conditionDetailList = ruleConditionDetailService.list(new QueryWrapper<>(new RuleConditionDetail().setConditionCode(ruleConditionDTO.getConditionCode())).orderByAsc("CONDITION_ORDER"));
                if (!conditionDetailList.isEmpty()) {
                    for (RuleConditionDetail detail : conditionDetailList) {
                        conditionDetailDTO = new ConditionDetailDTO();
                        conditionDetailDTO.setConditionDetailCode(detail.getConditionCode());
                        conditionDetailDTO.setConditionField(detail.getConditionField());
                        conditionDetailDTO.setConditionFlag(detail.getConditionFlag());
                        conditionDetailDTO.setCheckType(detail.getCheckType());
                        conditionDetailDTO.setSourceType(detail.getSourceType());
                        conditionDetailDTO.setDataSource(detail.getDataSource());
                        conditionDetailDTO.setConditionOrder(detail.getConditionOrder());
                        ruleConditionDTO.getConditionDetailList().add(conditionDetailDTO);
                    }
                }
            }
        }

        return Collections.unmodifiableMap(ruleConditionInfoMap);
    }

}
